<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
        .Mystyle {
            line-height: 38px;
            margin-left: 20px;
            border-bottom: 1.5px dashed deepskyblue;
        }
        .layui-form-item  p{
            float: left;
            font-size: 10px;
            color: #666;
            margin-left: 40%;
            margin-top: 7px;
        }

    </style>
</head>
<body>


<div class="layui-form layuimini-form" style="margin-left: -35px;">
    <div class="layui-form-item">
        <label class="layui-form-label">当前设备</label>
        <div class="layui-input-block">
            <!-- <input type="text" name="username" lay-verify="required" lay-reqtext="用户名不能为空" placeholder="请输入用户名" value="" class="layui-input"> -->
            <span class="Mystyle"></span>
            <!-- <tip>填写自己管理账号的名称。</tip> -->
        </div>
    </div>
    <!-- <div class="layui-form-item">
        <label class="layui-form-label required">性别</label>
        <div class="layui-input-block">
            <input type="radio" name="sex" value="男" title="男" checked="">
            <input type="radio" name="sex" value="女" title="女">
        </div>
    </div> -->
    <!-- <div class="layui-form-item">
        <label class="layui-form-label required">设备IP</label>
        <div class="layui-input-block">
            <input type="text" name="ip" lay-verify="ip" lay-reqtext="设备ip不能为空" placeholder="请输入ip" value="" class="layui-input" >
        </div>
    </div> -->
    <div class="layui-form-item">
        <label class="layui-form-label">采样间隔 (min)</label>
        <div class="layui-input-block">
            <input type="number"  name="samplecycle"   lay-verify="number"  placeholder="请输入采样周期" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">报警阈值(°C)</label>
        <div class="layui-input-block">
            <input type="text" name="alarmset" lay-verify="number"  placeholder="请输入报警阈值" value="" class="layui-input">
        </div>
    </div>
    <!-- <div class="layui-form-item">
        <label class="layui-form-label">发送周期 (个)</label>
        <div class="layui-input-block">
            <input type="text" name="transcycle" lay-verify="number"  placeholder="请输入发送周期" value="" class="layui-input">
        </div>
    </div> -->
    <div class="layui-form-item" style="margin-left: 50px;">
        <div class="layui-inline">
            <label class="layui-form-label" style="inline-size: max-content;">发送周期(min)  =</label>
            <div class="layui-input-inline" style="width: 100px;">
                <input type="text" name="price_min" placeholder="联动采样周期"  autocomplete="off" class="layui-input" disabled>
            </div>
            <div class="layui-form-mid">X</div>
            <div class="layui-input-inline" style="width: 100px;">
                <input type="text" name="price_max" placeholder="个数" lay-verify="number" autocomplete="off" class="layui-input">
            </div>
            <p>采样周期 = 采样间隔 x 个数</p>
        </div>
    </div>
     <div class="layui-form-item">
        <label class="layui-form-label">当前采样周期数</label>
        <div class="layui-input-block">
            <input type="text" name="transcycle" placeholder="请输入发送周期" value="" class="layui-input" disabled>
        </div>
        <p>该终端当前采样值，仅只读不允许修改！</p>
    </div>
    <!-- <div class="layui-form-item layui-form-text">·
        <label class="layui-form-label">备注信息</label>
        <div class="layui-input-block">
            <textarea name="remark" class="layui-textarea" placeholder="请输入备注信息"></textarea>
        </div>
    </div> -->

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="saveBtn" style=" margin-top: 10px; border-radius: 20px; margin-left: 199px;">更改配置</button>
        </div>
    </div>
</div>
</div>
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

            $("input[name='samplecycle']").bind("input propertychange",function(event){
                        var text_val = $("input[name='samplecycle']").val();
                         $("input[name='price_min']").val(text_val)
            });

            var token = sessionStorage.getItem('newtoken');
            var config =JSON.parse(sessionStorage.getItem('SaveConfigName'));
            var configData =JSON.parse(sessionStorage.getItem('SaveConfigData'));
            var deviceCode = sessionStorage.getItem('SavedeviceCode');

           /*******初始化表单******/
            $('.Mystyle').text(config.text);
            if(configData != null){
                    // $("input[name='ip']").val(configData.ip)
                    $("input[name='samplecycle']").val(configData.samplecycle / 60)
                    $("input[name='alarmset']").val(configData.alarmset)
                    $("input[name='price_min']").val((configData.samplecycle / 60))
                    $("input[name='transcycle']").val((configData.transcycle / 60))
            }
           /*********************/
        
           

        //监听提交

        form.on('submit(saveBtn)', function (data) {

            var str = data.field;
            var samplecycle = str.samplecycle * 60 ;
            var transcycle = (str.price_max * str.price_min) * 60 ;
            var l = '106.14.62.86'
        
            if(configData == null){
                    
                var index =  layer.confirm('确定提交数据吗？', {
                        btn: ['确定','取消'] //按钮
                        }, function(){
                                  
                                    $.ajax({          
                                    url: '/oms-web/wirelessTemp/saveConfigure?rtuno='+config.termNumber + '&ip=' + l + '&samplecycle=' + samplecycle + '&transcycle=' + transcycle + '&alarmset=' + str.alarmset + '&token='+token,
                                    dataType:"json",  
                                    type:"post", 
                                    contentType:"application/json",
                                        success:function(data){ if (data.code == -1) {
                          window.location = '../login-1.html'
                      }
                                        
                                        if(data.code == 0){
                                                if(data.data != null && data.data != undefined){

                                                    layer.msg('配置成功', {icon: 1,time:1000},function(){
                                                        parent.layer.close(iframeIndex)
                                                    });
                                                }else{
                                                    layer.msg('配置失败', {icon: 2},function(){
                                                            parent.layer.close(iframeIndex2);
                                                    });
                                                }
                                        }
                                    },      
                                    error:function(){
                                        layer.msg('数据出错！')
                                    }

                                });
                                
                                // var iframeIndex = parent.layer.getFrameIndex(window.name);
                                // parent.layer.close(iframeIndex);

                        }, function(){
                                layer.msg('重新提交数据',{icon:2},{
                                });
                        });
            }else{
                // if(configData.ip == str.ip && configData.samplecycle == str.samplecycle && configData.alarmset == str.alarmset && configData.transcycle == str.transcycle){
                // layer.msg('未作出改动！', {icon: 5});
            // }else{

                var index =  layer.confirm('确定提交数据吗？', {
                        btn: ['确定','取消'] //按钮
                        }, function(){
                                  
                                    $.ajax({          
                                    url: '/oms-web/wirelessTemp/saveConfigure?rtuno='+config.termNumber + '&ip=' + l + '&samplecycle=' + samplecycle + '&transcycle=' + transcycle + '&alarmset=' + str.alarmset + '&token='+token,
                                    dataType:"json",  
                                    type:"post", 
                                    contentType:"application/json",
                                        success:function(data){ if (data.code == -1) {
                          window.location = '../login-1.html'
                      }
                                        
                                        if(data.code == 0){
                                                if(data.data != null && data.data != undefined){

                                                    layer.msg('配置成功', {icon: 1},function(){
                                                        parent.layer.close(iframeIndex)
                                                    });
                                                }else{
                                                    layer.msg('配置失败', {icon: 2},function(){
                                                            parent.layer.close(iframeIndex2);
                                                    });
                                                }
                                        }
                                    },      
                                    error:function(){
                                        layer.msg('数据出错！')
                                    }

                                });
                                
                                // var iframeIndex = parent.layer.getFrameIndex(window.name);
                                // parent.layer.close(iframeIndex);

                        }, function(){
                                layer.msg('重新提交数据',{icon:2},{
                                });
                        });
            //   } 
            }

           
            

 var iframeIndex = parent.layer.getFrameIndex(window.name);
        });

    });
</script>
</body>
</html>